package com.da.javatest.niuke.baodian;

/**
 * @author chenlida
 * @date 2020/10/4 15:11
 * https://www.nowcoder.com/practice/21f3a84300c94db092e0b5a7bf2d0ad1?tpId=8&tags=&title=&diffculty=0&judgeStatus=0&rp=1
 */
public class C5Zipper {
    public static void main(String[] args) {
        System.out.println(zipString("aabcccccaaa"));
        System.out.println(zipString("welcometonowcoderrrrr"));
    }

    public static String zipString(String iniString) {
        StringBuilder sb = new StringBuilder();
        char c = iniString.charAt(0);
        sb.append(c);
        int count = 1;
        for (int i = 1; i < iniString.length(); i++) {
            char indexChar = iniString.charAt(i);
            if (c == indexChar) {
                count++;
                if (i == iniString.length() - 1) {
                    sb.append(count);
                }
            } else {
                c = indexChar;
                sb.append(count);
                sb.append(c);
                count = 1;
                if (sb.length() >= iniString.length()) {
                    return iniString;
                }
            }
        }
        return sb.toString();
    }
}
